package org.vuzya.motivateme.activities;

import static org.vuzya.motivateme.helpers.ConstantHelper.EXTRA_BUSINESS_ID;

import org.vuzya.motivateme.R;
import org.vuzya.motivateme.db.DBHelper;
import org.vuzya.motivateme.helpers.AdapterHelper;
import org.vuzya.motivateme.helpers.QuickActionHelper;
import org.vuzya.motivateme.quickaction.QuickAction;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FinishedListActivity extends ListActivity implements QuickAction.OnActionItemClickListener {
	private static final String TAG = "MAIN_ACTIVITY";
	
	private QuickAction qa;
	private long currentItemId;
	private Cursor cursor;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        //setContentView(R.layout.list);

    	getListView().setBackgroundResource(R.color.background);
    	getListView().setCacheColorHint(0x00000000);

        DBHelper dbh = new DBHelper(this);
        try{
            bindList(dbh);
        } catch (Exception e) {
			Log.e(TAG, "DB fail at noCreate");
		} finally {
			dbh.close();
		}
        
        qa = new QuickAction(this);
        qa.setOnActionItemClickListener(this);
        QuickActionHelper.fillQuickAction(qa, QuickActionHelper.getQuickActionDoneBusinessListItems(getResources()));
    }

	@Override
	protected void onDestroy() {
		if(cursor != null){
			cursor.close();
		}
		super.onDestroy();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == RESULT_OK){
			DBHelper dbh = new DBHelper(this);
	        try{
	            bindList(dbh);
	        } catch (Exception e) {
				Log.e(TAG, "DB fail at onActivityResult");
			} finally {
				dbh.close();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		currentItemId = id;
		qa.show(v);
	}

	public void onItemClick(View v, int pos) {
		DBHelper dbh;
		Intent i;
		switch(pos){
		case 0: // undone
			dbh = new DBHelper(this);
			try{
				dbh.businessIsUndone(currentItemId);
				bindList(dbh);
			}catch (Exception e) {
				Log.d(TAG, "DB fails when deleting business entity");
			}finally{
				dbh.close();
			}
			break;
		case 1: // Progress
			i = new Intent(this, TableActivity.class);
			i.putExtra(EXTRA_BUSINESS_ID, currentItemId);
			startActivity(i);
			break;
		case 2: // Edit
			i = new Intent(this, EditBusinessActivity.class);
			i.putExtra(EXTRA_BUSINESS_ID, currentItemId);
			startActivityForResult(i, 0);
			break;
		case 3: // Delete
			dbh = new DBHelper(this);
			try{
				dbh.deleteBusiness(currentItemId);
				bindList(dbh);
			}catch (Exception e) {
				Log.d(TAG, "DB fails when deleting business entity");
			}finally{
				dbh.close();
			}
			break;
		}
	}

	private void bindList(DBHelper dbh){
		if (cursor != null){
			cursor.close();
		}
		cursor = dbh.getBusinesses(true);
        
        ListAdapter adapter = AdapterHelper.getBusinessListAdapter(this, cursor, true);
        setListAdapter(adapter);
	}
}